﻿#include "StdAfx.h"	  // for unicoude save   U҉U҉U

#include "TestOffSet.h"
#include "ProMdl.h"
#include <iostream>
#include <ProSurface.h>
#include <vector>
#include <ProWindows.h>
#include "witotk\wProSurface.hpp"
#include "witotk\wProContour.hpp"
#include "witotk\wProSelection.hpp"
#include "witotk\wProException.hpp"
#include "witotk\wProFeature.hpp"
#include "witotk\wProElempath.hpp"
#include "witotk\wProSection.hpp"
#include "TestSect.h"
#include "ProSecdim.h"
#include "ProUtil.h"

using namespace std;
using namespace witotk;


TestOffSet::TestOffSet(void)
{
}


TestOffSet::~TestOffSet(void)
{
}

ProError ProSurfaceContourVisitAction_1 
	( ProContour  p_contour,
	ProError    /*status*/,
	ProAppData  app_data
	)
{
	vector<ProContour> * ct = (vector<ProContour>*)app_data;
	ct->push_back(p_contour);
	return PRO_TK_NO_ERROR;
}

void TestOffSet::test1()
{

	ProError error = PRO_TK_NO_ERROR;

	ProMdl mdl(NULL);
	error = ProMdlCurrentGet(&mdl);
	ProSelection* sel = NULL;    
	int n = 0;
	error = ProSelect("surface",1,NULL,NULL,NULL,NULL,&sel,&n);
	if(error != PRO_TK_NO_ERROR || n != 1)
	{
		cout<<"ProSelect Error."<<endl;
		return ;
	}
	
	ProModelitem mdl_item;
	error = ProSelectionModelitemGet(sel[0],&mdl_item);
	if(error != PRO_TK_NO_ERROR )
	{
		cout<<"ProSelectionModelitemGet Error."<<endl;
		return ;
	}

	ProSurface suff;
	
	error = ProGeomitemToSurface(&mdl_item,&suff);
	if(error != PRO_TK_NO_ERROR )
	{
		cout<<"ProGeomitemToSurface Error."<<endl;
		return ;
	}
	wProSurface_ptr suf(new wProSurface(suff));
	/*
	ProGeomitemdata* p_data;
	error = ProSurfaceDataGet(suf.get(),&p_data);
	if(error != PRO_TK_NO_ERROR )
	{
		cout<<"ProSurfaceDataGet Error."<<endl;
		return ;
	}*/

	wProContourCollection_ptr contourCollection = suf->GetContours();
	/*
	vector<ProContour> contours;
	error = ProSurfaceContourVisit    (suf,ProSurfaceContourVisitAction_1 ,NULL,(ProAppData   )&contours);
	if(error != PRO_TK_NO_ERROR )
	{
		cout<<"ProSurfaceContourVisit Error."<<endl;
		return ;
	}*/	
	int wid(0);
	ProWindowCurrentGet(&wid);
	ProWindowClear(wid);

	for(size_t i = 0 ; i< contourCollection->size(); i++)
	{
		wProContour_ptr con = contourCollection[i];

		wProEdgeCollection_ptr eds = con->GetEdges(suf->data());
		for(size_t j = 0 ; j< eds->size(); j++)
		{
			wProEdge_ptr p_edg = eds[j];

			ProModelitem it = p_edg->ToGeomitem((ProSolid)mdl);

			ProSelection selectionct;
			error =  ProSelectionAlloc (NULL,&it,&selectionct);
			error =  ProSelectionHighlight(selectionct,PRO_COLOR_HIGHLITE);

			ProSelectionFree(&selectionct);
			
		}
		break;
	}

	/*for(vector<ProContour>::iterator it = contours.begin(); it != contours.end(); it++)
	{
		ProSelection selectionct;
		error =  ProSelectionAlloc (NULL,,&selectionct);
		error =  ProSelectionHighlight(selectionct,PRO_COLOR_HIGHLITE);

	}*/
		
		

}
int ProUtilSecEntityUseEdgeChain (ProSection section);
ProError ProTestSectionSolveRegen(ProSection section);

ProError wProTestBoxSectBuild(
    ProSection section,
    ProType sec_type
    )
{
    ProError status;
    Pro2dLinedef line;
    int ent_ids[7];
    double width,height,offset;
    Pro2dPnt dim_pnt;
    int entdim_ids[2];
    int dim_ids[5];
    double edge1[2][3],edge2[2][3],offsets[2];
    double sk_transf[4][4];
    double transform[4][4];
    double angle = 0.0;
    ProSectionPointType pnt_types[2];
    ProLine w_message;
    ProWSecerror sec_errors;

    width = 120.0;
    height = 100.0;


    offset = width/2.0;
    offsets[0] = offsets[1] = offset;


/*--------------------------------------------------------------------*\
    Define the lines of the box
\*--------------------------------------------------------------------*/
    line.type = PRO_2D_CENTER_LINE;
    line.end1[0] = 0.0;
    line.end1[1] = 0.0;
    line.end2[0] = 0.0;
    line.end2[1] = 1.0;
    status = ProSectionEntityAdd(section,(Pro2dEntdef *)&line,&ent_ids[0]);
	PROE_THROW(status,"ProSectionEntityAdd");

    line.type = PRO_2D_LINE;
    line.end1[0] = offsets[0];
    line.end1[1] = offsets[1];
    line.end2[0] = offsets[0];
    line.end2[1] = offsets[1] + height;

    status = ProSectionEntityAdd(section,(Pro2dEntdef *)&line,  &ent_ids[1]);
	PROE_THROW(status,"ProSectionEntityAdd");

    line.type = PRO_2D_LINE;
    line.end1[0] = offsets[0];
    line.end1[1] = offsets[1] + height;
    line.end2[0] = offsets[0] + width;
    line.end2[1] = offsets[1] + height;

    status = ProSectionEntityAdd(section,(Pro2dEntdef *)&line,  &ent_ids[2]);
	PROE_THROW(status,"ProSectionEntityAdd");

    line.type = PRO_2D_LINE;
    line.end1[0] = offsets[0] + width;
    line.end1[1] = offsets[1] + height;
    line.end2[0] = offsets[0] + width;
    line.end2[1] = offsets[1];

    status = ProSectionEntityAdd(section,(Pro2dEntdef *)&line,  &ent_ids[3]);
	PROE_THROW(status,"ProSectionEntityAdd");

	
    line.type = PRO_2D_LINE;
    line.end1[0] = offsets[0] + width;
    line.end1[1] = offsets[1];
    line.end2[0] = offsets[0];
    line.end2[1] = offsets[1];

    status = ProSectionEntityAdd(section,(Pro2dEntdef *)&line,
                                      &ent_ids[4]);
	PROE_THROW(status,"ProSectionEntityAdd");
	
/*--------------------------------------------------------------------*\
    Dimension the box
\*--------------------------------------------------------------------*/
   /* dim_pnt[0] = offsets[0] -1;
    dim_pnt[1] = offsets[1] + height/2;

    entdim_ids[0] = ent_ids[2];
    entdim_ids[1] = ent_ids[4];
    pnt_types[0] = PRO_ENT_WHOLE;
    pnt_types[1] = PRO_ENT_WHOLE;
    status = ProSecdimCreate(section, entdim_ids, pnt_types, 2,
                        PRO_TK_DIM_LINE_LINE,dim_pnt,&dim_ids[0]);
	PROE_THROW(status,"ProSecdimCreate");
    dim_pnt[0] = offsets[0] - width/2;
    dim_pnt[1] = offsets[1] + 1.0;
    entdim_ids[0] = ent_ids[2];
    pnt_types[0] = PRO_ENT_WHOLE;

    status = ProSecdimCreate(section,entdim_ids,pnt_types,1,
                                    PRO_TK_DIM_LINE, dim_pnt, &dim_ids[1]);
	PROE_THROW(status,"ProSecdimCreate");
/*--------------------------------------------------------------------*\
    Autodim section
\*--------------------------------------------------------------------*/
    ProTestSecerrAlloc(&sec_errors);
    ProStringToWstring(w_message, (char *)"ProSectionAutodim() errors.");
    status = ProSecerrorAdd(&sec_errors, 0, w_message, 0, -1, 0);
				PROE_THROW(status,"ProSecerrorAdd");
    status = ProSectionAutodim(section, &sec_errors);
				PROE_THROW(status,"ProSectionAutodim");
    if (status != PRO_TK_NO_ERROR)
        ProTestSecerrShow(sec_errors);
    ProTestSecerrFree(&sec_errors);

/*--------------------------------------------------------------------*\
    Solve and regenerate the section
\*--------------------------------------------------------------------*/
    ProTestSecerrAlloc(&sec_errors);
    status = ProSectionRegenerate(section,&sec_errors);

    if (status != PRO_TK_NO_ERROR)
    {
        ProTestSecerrShow(sec_errors);
        return(PRO_TK_GENERAL_ERROR);
    }
    ProTestSecerrFree(&sec_errors);

    return(PRO_TK_NO_ERROR);
}

int ProUtilSecEntityUseEdge(ProSection section);


void TestOffSet::test2()
{
	try
	{
		
		wProSelectionCollection_ptr selc = wProSelection::Select("feature",1);//选择草绘
		wProSelection_ptr sc = selc[0];
		wProFeature fetureSck = sc->GetModelitem();
		wProElemtree_ptr eletree = fetureSck.ExtractElemtree(NULL);//获取特征树
		
		//wPath wppth;
		wProElempath path;
		path.SetData(wPath()[ID_(PRO_E_STD_SECTION)][ID_(PRO_E_SKETCHER)]); //构建特征路径
		wProElement_ptr SKETCHER_ele = eletree->GetElement(path);		//获取草绘对象所在元素

		ProAppData ref = SKETCHER_ele->GetSpecialvalue(); //获取草绘对象
		ProSection psection = (ProSection)ref;
		wProSection section(psection);

		//ProUtilSecEntityUseEdgeChain(psection);
		//wProTestBoxSectBuild(psection,PRO_3DSECTION);

		//ProUtilSecEntityUseEdge(psection);

		wProSelectionCollection_ptr sc0 = wProSelection::Select( "edge",1);		//选择边
		wProSelectionCollection_ptr sc1 = wProSelection::Select( "edge",1);		//选择边
		wProSelectionCollection_ptr sc2 = wProSelection::Select( "edge_end",1);	//选择顶点
	
		section.EntityUseOffsetChain(*(sc0[0]),sc1[0]->data(),sc2[0]->data(),10); //构建偏移链
		//section.EntityUseCurveChain(sc0[0]->data(),sc1[0]->data(),sc2[0]->data());
		
		/*--------------------------------------------------------------------*\
			Autodim section
		\*--------------------------------------------------------------------*/
		wProWSecerror_ptr sec_errors(new wProWSecerror);
		sec_errors->Add(0, L"ProSectionAutodim() errors.");
		section.Autodim(*sec_errors);								//自动尺寸

		/*--------------------------------------------------------------------*\
			Solve and regenerate the section
		\*--------------------------------------------------------------------*/
		
		//wProWSecerror_ptr sec_errors1(new wProWSecerror);
		//section.Solve(*sec_errors1);

		wProWSecerror_ptr sec_errors2(new wProWSecerror);		   //再生
		section.Regenerate(*sec_errors2);

		fetureSck.Redefine(NULL,eletree->data());				   //重定义特征
		 //   U҉U҉U
	}
	catch (ProException& e)
	{
		wcout<<"error:"<<e.GetDescribe()<<endl;
		std::cout << typeid(e).name() << std::endl;  
	}

}
